package com.translate.web.service.impl;

import java.util.List;
import org.springframework.stereotype.Service;

import com.translate.web.dao.CustomerPoolMemberMapper;
import com.translate.entity.CustomerPoolMember;
import com.translate.web.param.contract.ICustomerPoolAddMemberParam;
import com.param.contract.IIdParam;
import com.translate.web.service.ICustomerPoolStaffService;
import com.basic.service.impl.BaseServiceImpl;

@Service
public class CustomerPoolStaffServiceImpl extends BaseServiceImpl<CustomerPoolMember>
		implements ICustomerPoolStaffService {

	public CustomerPoolStaffServiceImpl(CustomerPoolMemberMapper mapper) {
		super(mapper);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int addFollowPerson(int customerId, long followId) {
		return 0;

//		CustomerPoolMember CustomerPoolMember = new CustomerPoolMember();
//		
//		CustomerPoolMember.setMemId(customerId);
//		
//		CustomerPoolMember.setFollowMe((int)followId);
//		
//		return this.insert(CustomerPoolMember);

	}

	@Override
	public <S extends ICustomerPoolAddMemberParam> int addStaff(List<S> list, int createUserId, int seaId) {

		if (!checkHasManagerAndStaff(list)) {
			throw new Error("没有管理员");
		}
		
		int size = list.size();

		CustomerPoolMember[] customerPoolMembers = new CustomerPoolMember[size];

		CustomerPoolMember customerPoolMember;

		S customerPoolAddMemberParam;

		for (int i = 0; i < size; i++) {

			customerPoolAddMemberParam = list.get(i);

			customerPoolMember = new CustomerPoolMember();

			toEntity(customerPoolMember, customerPoolAddMemberParam, seaId, createUserId);

			customerPoolMembers[i] = customerPoolMember;

		}

		return this.batchInsert(customerPoolMembers);
	}

	// 验证是否有管理员
	protected <S extends ICustomerPoolAddMemberParam> boolean checkHasManagerAndStaff(List<S> list) {

		int managerFalg = 0, memFlag = 0;

		int memStatus = 0;

		for (S s : list) {

			memStatus = s.getManagerStatus();

			if (1 == memStatus) {

				managerFalg = 1;

				continue;
			}
			if (0 == memStatus) {
				memFlag = 1;
				continue;
			}
		}

		return 0 != managerFalg && 0 != memFlag;
	}

	@Override
	public <S extends ICustomerPoolAddMemberParam> int updateStaff(List<S> list, int createUserId, int seaId) {

		if (!checkHasManagerAndStaff(list)) {
			throw new Error("没有管理员");
		}
		
		return this.updateOrInsertOrDeleteByCondition(list, seaId, createUserId);

	}

	/**
	 * 比较更新主要条件
	 * 
	 * @return
	 */
	protected <S> boolean compareMasterCondition(S condition, CustomerPoolMember data) {

		ICustomerPoolAddMemberParam customerPoolAddMemberParam = (ICustomerPoolAddMemberParam) condition;

		Byte managerStatus = customerPoolAddMemberParam.getManagerStatus();

		Byte memType = customerPoolAddMemberParam.getMemType();

		return 0 == data.getMemId().compareTo(customerPoolAddMemberParam.getMemId()) && memType == data.getMemType()
				&& managerStatus == data.getManagerStatus();

	}

	protected CustomerPoolMember getEntityInstance() {
		return new CustomerPoolMember();
	}

	protected int getId(CustomerPoolMember data) {
		return data.getId();
	}

	protected <S> void toEntity(CustomerPoolMember customerPoolMember, S obtainedFromTheMap, int assocId, int userId) {

		ICustomerPoolAddMemberParam customerPoolAddMemberParam = (ICustomerPoolAddMemberParam) obtainedFromTheMap;

		customerPoolMember.setPoolId(assocId);

		Integer id = null;
		
		if (obtainedFromTheMap instanceof IIdParam) {
			id = ((IIdParam) obtainedFromTheMap).getId();
			
			if (null != id) {
				customerPoolMember.setId(id);
			}
		}

		Byte managerStatus;

		byte memType = 1;
		byte manager = 1;

		Byte memValue;

		managerStatus = customerPoolAddMemberParam.getManagerStatus();

		memValue = customerPoolAddMemberParam.getMemType();
		if (0 == managerStatus.compareTo(manager) && 0 == memValue.compareTo(memType)) { // 是管理员且是部门
			throw new Error("部门不能是管理员");
		}

		customerPoolMember.setManagerStatus(managerStatus);

		String deptName = customerPoolAddMemberParam.getDeptName();
		if (null == deptName) {
			deptName = "";
		}

		customerPoolMember.setCreateUserId(userId);
		customerPoolMember.setDeptName(deptName);

		customerPoolMember.setMemId(customerPoolAddMemberParam.getMemId());

		customerPoolMember.setMemType(memType);
	}

//	private RedisTemplate<String, Object> redisTemplate;
//
//
//	@Autowired
//	public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
//		this.redisTemplate = redisTemplate;
//	}

//	@Override
//	public <P> int getInsertIdByData(ICustomerParam<P> customerParam) {
//		
//		prepareData(customerParam);
//		
//		return this.parseInsert(customerParam, Customer.class);
//	}
//	
//	protected <P> void prepareData(ICustomerParam<P> customerParam) {
//		
//		if (null == customerParam.getEmail()) {
//			customerParam.setEmail("");
//		} 
//		
//		if (null == customerParam.getTelephone()) {
//			customerParam.setTelephone("");
//		}
//		
//	}
//	
//	protected int getId(Customer data) {
//		return data.getId();
//	}
//
//	@Override
//	public <P> int getUpdateByData(ICustomerParam<P> customerParam) {
//		prepareData(customerParam)
//		;
//		
//		return this.parseUpdate(customerParam, Customer.class);
//	}

}
